package com.inossem.warehousestorage_inossem.activity.receive;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.receive.extra.ReceiveExtra;
import com.inossem.warehousestorage_inossem.bean.receive.ConfirmBean;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveReceiveItemBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.receive.ReceiveConfirmRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.ReceiveUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

public class ReceiveConfirmActivity extends BaseActivity {

    private static final String SUCCESS = "1";//成功

    private static final String FAIL = "0";//失败

    private static final boolean DRAFT = false;//草稿

    private static final boolean CONFIRM = true;//提交

    private TextView title, saveDraft, confirm, receiveNumber, purchase, supplier, submitLine;

    private PercentRelativeLayout submitOverviewLayout;

    private boolean mIsInsert;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_receive_confirm);
        initView(ReceiveConfirmActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(ReceiveConfirmActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        saveDraft = (TextView) activity.findViewById(R.id.saveDraft);
        confirm = (TextView) activity.findViewById(R.id.confirm);
        receiveNumber = (TextView) activity.findViewById(R.id.receiveNumber);
        purchase = (TextView) activity.findViewById(R.id.purchase);
        supplier = (TextView) activity.findViewById(R.id.supplier);
        submitLine = (TextView) activity.findViewById(R.id.submitLine);
        submitOverviewLayout = (PercentRelativeLayout) activity.findViewById(R.id.submitOverviewLayout);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_receive_confirm_title));
        saveDraft.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                confirmOrDraft(activity, DRAFT, mIsInsert);
            }
        });
        confirm.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                confirmOrDraft(activity, CONFIRM, mIsInsert);
            }
        });
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_add_receiver_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        getData(activity);
        getExtra(activity);
    }

    private void getExtra(Activity activity) {
        mIsInsert = activity.getIntent().getBooleanExtra(ReceiveExtra.IS_INSERT, false);
    }

    private void confirmOrDraft(Context context, boolean isSaveCommit, boolean isInsert) {
        ReceiveConfirmRequestBean params = ReceiveUtils.getReceiveConfirmRequestBean(context, isSaveCommit);
        if (isSaveCommit) {
            ReceiveConfirmRequest.request(context, params, isInsert, new GsonBeanListener<ConfirmBean>() {
                @Override
                public void onSuccess(ConfirmBean bean) {
                    if (SUCCESS.equals(bean.getData().getCode())) {
                        SPUtils.removeReceiveInformation(context);
                        Intent intent = new Intent(context, ReceiveResultActivity.class);
                        intent.putExtra(ReceiveExtra.MESSAGE, bean.getData().getMsg());
                        intent.putExtra(ReceiveExtra.RECEIVE_NUMBER, params.getReceiveNumber());
                        intent.putExtra(ReceiveExtra.IS_CONFIRM, true);
                        startActivity(intent);
                    } else {
                        if (!TextUtils.isEmpty(bean.getData().getMsg())) {
                            Utils.middleToast(context, bean.getData().getMsg());
                        }
                    }
                }
            });
        } else {
            ReceiveConfirmRequest.request(context, params, isInsert, new GsonBeanListener<ConfirmBean>() {
                @Override
                public void onSuccess(ConfirmBean bean) {
                    if (SUCCESS.equals(bean.getData().getCode())) {
                        SPUtils.removeReceiveInformation(ReceiveConfirmActivity.this);
                        Intent intent = new Intent(context, ReceiveResultActivity.class);
                        intent.putExtra(ReceiveExtra.MESSAGE, bean.getData().getMsg());
                        intent.putExtra(ReceiveExtra.RECEIVE_NUMBER, params.getReceiveNumber());
                        intent.putExtra(ReceiveExtra.IS_CONFIRM, false);
                        startActivity(intent);
                    } else {
                        if (!TextUtils.isEmpty(bean.getData().getMsg())) {
                            Utils.middleToast(context, bean.getData().getMsg());
                        }
                    }
                }
            });
        }
    }

    private void getData(Context context) {
        ReceiveReceiveItemBean.DataBean data = ReceiveUtils.getReceiveReceiveItemBean(context).getData();
        receiveNumber.setText(data.getReceiveNumber());
        purchase.setText(data.getPurchaseNumber() + " - " + data.getPurchaseType());
        supplier.setText(data.getSupplier());
        submitLine.setText(data.getGoodsList().size() + "");
        submitOverviewLayout.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getOverviewDialog(context, ReceiveUtils.getOverViewBeanWithGoodsList(data.getGoodsList())).show();
            }
        });
    }

}
